package com.weakteam.orderonline.adminconsole.gwt.client.util;

import com.google.gwt.http.client.*;
import com.google.gwt.json.client.JSONArray;
import com.google.gwt.json.client.JSONObject;
import com.google.gwt.json.client.JSONParser;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Max on 03.04.2014 for OrderOnline.
 */
public class Requester {

    private List<Responser> responsers = new ArrayList<>();

    public void addResponser(Responser responser) {
        responsers.add(responser);
    }

    public void getCategoryAll() {
        this.doGetRequest(WebContext.getInstance().getRootContext() + "getMenuData");
    }

    public void getGoodsAll(int categoryId) {
        this.doGetRequest(WebContext.getInstance().getRootContext() + "getGoodsData?menuId=" + categoryId);
    }

    public void getPlacesAll() {
        this.doGetRequest(WebContext.getInstance().getRootContext() + "getPlacesData");
    }

    public void getOrdersAll() {
        this.doGetRequest(WebContext.getInstance().getRootContext() + "getOrdersData");
    }

    public void setCategoryAll(JSONArray data) {
        if (data.size()>0) {
            StringBuffer postData = new StringBuffer().append(URL.encode("data")).append("=").append(URL.encode(data.toString()));
            this.doPostRequest(WebContext.getInstance().getRootContext() + "saveMenuData",postData.toString());
        }
    }

    public void setGoodsAll(JSONArray data) {
        if (data.size()>0)  {
            StringBuffer postData = new StringBuffer().append(URL.encode("data")).append("=").append(URL.encode(data.toString()));
            this.doPostRequest(WebContext.getInstance().getRootContext() + "saveGoodsData",postData.toString());
        }
    }

    public void setCategoryOne(JSONObject obj) {
        StringBuffer postData = new StringBuffer().append(URL.encode("data")).append("=").append(URL.encode(obj.toString()));
        this.doPostRequest(WebContext.getInstance().getRootContext() + "saveMenuDataOne",postData.toString());
    }

    public void setGoodsOne(JSONObject obj) {
        StringBuffer postData = new StringBuffer().append(URL.encode("data")).append("=").append(URL.encode(obj.toString()));
        this.doPostRequest(WebContext.getInstance().getRootContext() + "saveGoodsDataOne",postData.toString());
    }

    public void setPlaceOne(JSONObject obj) {
        StringBuffer postData = new StringBuffer().append(URL.encode("data")).append("=").append(URL.encode(obj.toString()));
        this.doPostRequest(WebContext.getInstance().getRootContext() + "savePlacesDataOne",postData.toString());
    }

    public void deleteCategoryById(Integer id) {
        StringBuffer postData = new StringBuffer().append(URL.encode("id")).append("=").append(URL.encode(id.toString()));
        this.doPostRequest(WebContext.getInstance().getRootContext() + "deleteMenuData",postData.toString());
    }

    public void deleteGoodsById(Integer id) {
        StringBuffer postData = new StringBuffer().append(URL.encode("id")).append("=").append(URL.encode(id.toString()));
        this.doPostRequest(WebContext.getInstance().getRootContext() + "deleteGoodsData",postData.toString());
    }

    public void deletePlaceById(Integer id) {
        StringBuffer postData = new StringBuffer().append(URL.encode("id")).append("=").append(URL.encode(id.toString()));
        this.doPostRequest(WebContext.getInstance().getRootContext() + "deletePlacesData",postData.toString());
    }

    private void doGetRequest(String url) {
        RequestBuilder builder = new RequestBuilder(RequestBuilder.GET, url);
        try {
            builder.sendRequest(null, new RequestCallback() {
                @Override
                public void onError(Request request, Throwable exception) {
                    ConsoleLog.getInstance().error("loadData onError: " + exception);
                }
                @Override
                public void onResponseReceived(Request request, Response response) {
                    if (200 == response.getStatusCode()) {
                        String json = response.getText();
                        ConsoleLog.getInstance().debug("loadDataResponse " + json);
                        JSONArray data = (JSONArray) JSONParser.parseLenient(json);
                        for (Responser responser : responsers) {
                            responser.handleGetResponse(data);
                        }
                    }
                }
            });
        } catch (RequestException exception) {
            ConsoleLog.getInstance().error("loadData catch error: " + exception);
        }
    }

    // sendData
    private void doPostRequest(String url, String postData) {
        RequestBuilder builder = new RequestBuilder(RequestBuilder.POST,url);
        builder.setHeader("Content-type", "application/x-www-form-urlencoded");
        try {
            builder.sendRequest(postData, new RequestCallback() {
                @Override
                public void onError(Request request, Throwable exception) {
                    ConsoleLog.getInstance().error("sendData onError: " + exception);
                }
                @Override
                public void onResponseReceived(Request request, Response response) {
                    ConsoleLog.getInstance().debug("data post responseCode = "+response.getStatusCode());
                    for (Responser responser : responsers) {
                        responser.handlePostResponse(response.getStatusCode());
                    }
                }
            });
        } catch (RequestException exception) {
            ConsoleLog.getInstance().error("sendData catch error: " + exception);
        }
    }

}
